An object-oriented program is a program used to control the execution of a digital computer, where the program is created using a set of language tools, abstractions and constructs that support a particular form of user-defined types, called "classes". Each such type associates a collection of declared data with a set of operations on that data. Variables, or instances, of such types in the running programs are called objects.
Classes can be related to one another by inheritance. The properties, behaviours, data, and operations of a parent, or "base", class may be inherited without modification by some child, or "derived" class, or the behaviours, properties, and operations may be selectively refined under control of the programmer. When defining a derived class, one may start by building on an existing base class which is similar to the one to be created. The derived class inherits the implementation and behaviour of the base class, except as modified by amendments detailed in the derived class definition. Several classes can inherit the behaviours of a common parent, and a derived class may inherit from more than one base class. As examples, squares can inherit from rectangles, circles from ellipses, and stacks and queues from collections.
A member function is a function or operation defined as a behaviour of some user-defined type where that type participates in a hierarchy of other types. A virtual function is a member function with the added property that the specific identity of the virtual function, when invoked by name, is to be determined at execution time as a function of the type of the object with which it is associated. This is in contrast to member functions that are not virtual functions, whose specific identity, when invoked by name, is determined at compilation time as a function of the declared type of the variable used to access the object.
Each of several types in an inheritance hierarchy may contain its own definition of a member function where the name is common to all such types. If the function is a virtual function, as determined by and under the control of the programmer, then the function that is invoked at execution time will be determined from the type of the object which is the primary operand of the function instead of from the type of declared name that designates the object on which the function is to operate. Calls to virtual functions can be implemented as indirect calls through a table, called a virtual function table. Virtual function tables are created by the compiler for each user-defined type or class whose implementation requires such tables.
In the C++ language, the compiler defines an object data structure for each class. If a class introduces a virtual function or has an overriding function, then the compiler allocates a virtual function table for the class. In the C++ language, an override function is a member function of a class that has the same name, same number and same types of parameters as a member function declared with the virtual keyword in a base class of the class. The class object data structure contains the layout of the data members and a virtual function table pointer. The virtual function table pointer is initialized during run time to point to the virtual function table associated with the class.
A virtual function table for a class contains addresses of the virtual function members that are to be invoked at execution time when the class is the type of the object that is the primary operand of the function. The compiler and linker are jointly responsible for initializing a virtual function table with the addresses appropriate for its class. The virtual function table is used at run time to invoke the virtual functions indirectly. Each base class with a virtual function member has an associated virtual function table. Derived classes may have more than one virtual function table, since one may be associated with each base class, and their object data structures may, therefore, contain more that one virtual function table pointer. After memory for an object is allocated, the virtual function table pointers are initialized with the addresses of the associated virtual function table. Thus, all objects of the same class type point to the same virtual function tables. To implement the invoking of a virtual function, the compiler generates code to access the virtual function member through the virtual function table.
The C++ object oriented programming language, in particular, provides a powerful and popular virtual function feature. One virtual function overrides another function in accordance with specific rules that are language dependent. Under certain circumstances, the use of the virtual function feature can cause very large executable files to be generated for what should be relatively simple programs. This results in an unnecessary use of disk space for storing the program, and memory for executing it.
This situation commonly results from the established practice of using virtual function tables, pointed to by virtual function pointers embedded in the objects, in the runtime dispatching of virtual functions. These tables typically contain references to all virtual functions that might conceivably be dispatched in any application containing the object. In some applications this may result in unnecessarily linking in definitions for virtual functions that will never be called, and it is this redundancy that the present invention is directed to eliminating.
U.S. Pat. No. 5,247,678 issued Sep. 21, 1993 to Texas Instruments Inc. describes a multiprocessor system that links extended functions. The patent describes a dynamic linker that determines whether there are any functions loaded in the active memory which are not needed by the application program and, if so, discards them from the active memory. However, this patent does not teach a process where inactive functions are removed from the program itself as in the present invention.
U.S. Pat. No. 5,297,284 issued Mar. 22, 1994 to Microsoft Corp. describes a method simply for implementing virtual functions and virtual base classes in a compiler for an object-oriented programming language. This patent also does not consider the problem created by maintaining references to uncallable virtual functions in the compiled and linked program which of the present invention addresses.
U.S. Pat. No. 5,327,562 issued Jul. 5, 1994 to Microsoft Corp. describes a method for implementing virtual functions in a compiler for an object-oriented programming language. The patent does not describe any mechanism for removing references to uncallable virtual functions in the compiled and linked program.
By contrast, a primary purpose of the present invention is to remove uncallable virtual functions from a compiled and linked program.
A secondary purpose of the present invention is to avoid maintaining calls to virtual functions in a compiled and linked program when it can be determined that those virtual functions are uncallable.
To achieve this and other objects, the present invention provides means to reduce the negative impact of using virtual functions in the C++ language by identifying and removing references to unused virtual functions from the executable code. This is achieved by tagging the definitions and uses of virtual functions during compilation in such a manner that the linker can identify those functions which cannot be called in the application and so can ignore or exclude the definitions of those virtual functions.
Accordingly, in a computing environment having compiler means for compiling an object oriented source program into object modules and linker means for linking the object modules into an executable file provided with means including means for initializing virtual function tables for invoking virtual functions at runtime, a method is provided for improving the execution efficiency of the executable file. The method includes the steps of generating control data identifying each virtual function table during compilation wherein the control data is accessible during linking for identifying virtual functions as potentially callable, and linking only virtual functions identified as potentially callable.
Preferably, the step of generating control data consists of compiling a list of defined introducers for each virtual function encountered during compilation of the source program, compiling a list of called introducers, each called introducer identifying a group of virtual functions which includes a function that is actually called and embedding the lists as the control data records in object modules. Preferably, the step of linking only virtual functions identified as potentially callable consists of accumulating a list of virtual functions that are only referenced through a virtual function table, and linking pointers to virtual functions potentially used as determined from the lists of defined introducers and called introducers embedded as the control data records, and the list of virtual functions compiled during linking.
An improved system for converting source code written in an object oriented language into an executable file is also provided. The improvement consists of means for generating a list of defined introducers that each identifying a group of virtual functions, means for maintaining a list of virtual functions that are only referenced through a virtual function table, means for generating a list of called introducers that each identifying a group of virtual functions potentially called and means for generating the executable file to exclude those virtual functions that are only referenced through a virtual function table and are not identified by the lists of defined introducers, called introducers and virtual functions.
The invention is also directed to a mechanism for improving execution efficiency of machine executable code derived from a source program written in a compilable object oriented programming language. The mechanism has a compiler adapted to tag definitions and uses of virtual functions during compilation, and linking means adapted to identify tagged virtual functions, to link definitions of tagged definitions that can be called and to nullify the definitions of said tagged virtual functions that cannot be called.
Finally, the present invention provides computer program product that includes a computer usable medium having new, useful and nonobvious combination of computer readable program code means programmed so as to cause a computer to perform the method described above.